//
//  PlayResultLayer.cpp
//  NewMath
//
//  Created by Ngô Hoàng Nam on 1/3/15.
//
//

#include "PlayCongratulationsLayer.h"
#include "../PlayManager.h"

#include "../../../Objects/Ghost/Play/GhostResult.h"
#include "../../Level/LevelManager.h"
#include "../../../Support/GameSupport.h"
#include "../../../Player/PlayerInfo.h"
#include "../../../Framework/SceneManager.h"
#include "../../../Data/GameData.h"
#include "../../../Framework/SoundManager.h"

#include <cocostudio/cocostudio.h>
using namespace cocostudio;

bool PlayCongratulationsLayer::isProcessing = false;
PlayCongratulationsLayer::PlayCongratulationsLayer(Node* congratulationsNode, Size screenSize)
{
	m_screenSize = screenSize;
	m_nodeRoot = congratulationsNode;
	m_nodeRoot->retain();
	this->m_Win_1 = congratulationsNode->getChildByName("pnl_win_1");
	this->m_Win_n = congratulationsNode->getChildByName("pnl_win_n");
	this->m_Lose = congratulationsNode->getChildByName("pnl_lose");
	this->m_PlayManager = (PlayManager*)(congratulationsNode->getParent()->getParent());
	this->m_PlayManager->retain();
	isProcessing = false;

}
void PlayCongratulationsLayer::initalize(EGameStatus m_EGameStatus, std::vector<std::string> list_equation, int numStar = 0)
{
	this->m_star = numStar;

	if (list_equation.size() > 1)
	{
		initWinN(list_equation);
		return;
	}
	if (m_EGameStatus == EGameStatus::kGhostStatus_Fail)
	{
		initLose(list_equation);
		return;
	}
	if (m_EGameStatus == EGameStatus::kGhostStatus_Correct)
	{
		std::string equation = list_equation.at(0);
		initWin1(equation);
		return;
	}


}

void PlayCongratulationsLayer::initLose(std::vector<std::string> list_equation)
{

	Text* lblLevel = static_cast<Text*>((m_Lose->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_banner)))->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblLevel)));

	lblLevel->setText(String::createWithFormat("%d", LevelManager::s_ID)->getCString());
	//lblLevel->setString();



	Sprite* background = static_cast<Sprite*>(m_Lose->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_sprBackground)));

	Button* btnReplay = static_cast<Button*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_btnReplay)));
	btnReplay->addClickEventListener(CC_CALLBACK_1(PlayCongratulationsLayer::btnReplay_Tap, this));
	btnReplay->setPressedActionEnabled(true);


	Button* btnBack = static_cast<Button*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_btnBack)));
	btnBack->addClickEventListener(CC_CALLBACK_1(PlayCongratulationsLayer::btnBack_Tap, this));
	btnBack->setPressedActionEnabled(true);

	if (list_equation.size() == 1)
	{
		Text* lblEquation = static_cast<Text*>((background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_cloud)))->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblEquationInCloud)));


		const std::string equa = list_equation.at(0);
		lblEquation->setString(list_equation.at(0));
	}
	else
	{
		Node* nodeEquation = static_cast<Node*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblEquation)));


	}
	m_Lose->setVisible(true);
	m_Win_1->setVisible(false);
	m_Win_n->setVisible(false);


}

void PlayCongratulationsLayer::initWin1(std::string equation)
{

	Text* lblLevel = static_cast<Text*>((m_Win_1->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_banner)))->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblLevel)));
	lblLevel->setString(String::createWithFormat("%d", LevelManager::s_ID)->getCString());



	Sprite* background = static_cast<Sprite*>(m_Win_1->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_sprBackground)));

	Button* btnReplay = static_cast<Button*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_btnReplay)));
	btnReplay->addClickEventListener(CC_CALLBACK_1(PlayCongratulationsLayer::btnReplay_Tap, this));
	btnReplay->setPressedActionEnabled(true);
	Button* btnBack = static_cast<Button*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_btnBack)));
	btnBack->addClickEventListener(CC_CALLBACK_1(PlayCongratulationsLayer::btnBack_Tap, this));
	btnBack->setPressedActionEnabled(true);
	Button* btnNext = static_cast<Button*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_btnNext)));
	btnNext->addClickEventListener(CC_CALLBACK_1(PlayCongratulationsLayer::btnNext_Tap, this));
	btnNext->setPressedActionEnabled(true);

	if (m_star < 3)
	{
		Sprite* star1 = static_cast<Sprite*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_sprStar3)));
		star1->setVisible(false);
	}
	if (m_star < 2)
	{
		background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_sprStar2))->setVisible(false);
	}
	if (m_star < 1)
	{
		background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_sprStar1))->setVisible(false);
	}
    PlayerInfo::getInstance()->level->setLevelStar(LevelManager::s_mode, LevelManager::s_ID, m_star);
	Text* lblEquation = static_cast<Text*>((background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_cloud)))->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblEquationInCloud)));
	std::string ste = lblEquation->getString();

	lblEquation->setString(equation);

	Text* lblScore = static_cast<Text*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblScore)));
	lblScore->setString(String::createWithFormat("Score: %d", m_PlayManager->m_list_score.at(0))->getCString());
	int highScore = PlayerInfo::getInstance()->level->getLevelScore(LevelManager::s_mode, LevelManager::s_ID);
	int currScore = m_PlayManager->m_list_score.at(0);
	if (highScore >= currScore)
	{
		background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblHighScore))->setVisible(false);
	}

	m_Lose->setVisible(false);
	m_Win_n->setVisible(false);
	m_Win_1->setVisible(true);


}

void PlayCongratulationsLayer::initWinN(std::vector<std::string> list_equation)
{
	Text* lblLevel = static_cast<Text*>((m_Win_n->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_banner)))->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblLevel)));
	lblLevel->setString(String::createWithFormat("%d", LevelManager::s_ID)->getCString());



	Sprite* background = static_cast<Sprite*>(m_Win_n->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_sprBackground)));

	Button* btnReplay = static_cast<Button*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_btnReplay)));
	btnReplay->addClickEventListener(CC_CALLBACK_1(PlayCongratulationsLayer::btnReplay_Tap, this));
	btnReplay->setPressedActionEnabled(true);
	Button* btnBack = static_cast<Button*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_btnBack)));
	btnBack->addClickEventListener(CC_CALLBACK_1(PlayCongratulationsLayer::btnBack_Tap, this));
	btnBack->setPressedActionEnabled(true);
	Button* btnNext = static_cast<Button*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_btnNext)));
	btnNext->addClickEventListener(CC_CALLBACK_1(PlayCongratulationsLayer::btnNext_Tap, this));
	btnNext->setPressedActionEnabled(true);

	//Caculate star
	int size = m_PlayManager->m_list_star.size();
	m_star = 0;
	for (int i = 0; i < size; i++)
	{
		m_star += m_PlayManager->m_list_star.at(i);
	}
	m_star /= size;



	if (m_star < 3)
	{
		Sprite* star1 = static_cast<Sprite*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_sprStar3)));
		star1->setVisible(false);
	}
	if (m_star < 2)
	{
		background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_sprStar2))->setVisible(false);
	}
	if (m_star < 1)
	{
		background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_sprStar1))->setVisible(false);
	}
     PlayerInfo::getInstance()->level->setLevelStar(LevelManager::s_mode, LevelManager::s_ID, m_star);
	Node* lblEquation = static_cast<Node*>((background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblEquation))));
	//std::string ste= lblEquation->getString();
	lblEquation->removeAllChildren();
	Vector<MenuItem*> list_item;
	ValueVector rows;

	int listSize = (int)list_equation.size();
	for (int i = 0; i < listSize; i++)
	{
		//        if(rows.size() < 2)
		//            rows.push_back()
		MenuItemLabel* equation = MenuItemLabel::create(Label::create(list_equation.at(i), kFont_ADLIBN, 60));
		if (m_PlayManager->m_gameMode == EGameMode::kMode_Hard)
		{
			switch (m_PlayManager->m_list_AnswerStatus.at(i)) {
			case kGhostStatus_Correct:
				equation->getLabel()->setColor(GameSupport::getGhostColor(EGhostColor::kGhostColor_Green));
				break;
			case kGhostStatus_Fail:
				equation->getLabel()->setColor(GameSupport::getGhostColor(EGhostColor::kGhostColor_Red));
			default:
				break;
			}
		}
		else
			equation->getLabel()->setColor(GameSupport::getGhostColor(EGhostColor::kGhostColor_Green));
		list_item.pushBack(equation);
	}
	float column = (float)list_equation.size() / 4.0f;
	if (column != (int)column)
		column = (int)column + 1;


	Menu* menuEquation = Menu::createWithArray(list_item);
	if (list_item.size() > 5)
	{
		for (int i = 0; i < size; i++)
		if (i < size / 2)
			list_item.at(i)->setAnchorPoint(Vec2(0.7f, 0.5f));
		else
			list_item.at(i)->setAnchorPoint(Vec2(0.3f, 0.5f));
		menuEquation->alignItemsInRows((int)list_equation.size() / 2, (int)list_equation.size() / 2, NULL);
		menuEquation->setPosition(Vec2(0, menuEquation->getContentSize().height / 3));
	}
	else
	{
		menuEquation->setPosition(Vec2(0, -menuEquation->getContentSize().height / 9));
		menuEquation->alignItemsVertically();
	}
	//menuEquation->set
	lblEquation->addChild(menuEquation);
	Text* lblScore = static_cast<Text*>(background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblScore)));
	int currScore = 0;
	for (int i = 0; i < m_PlayManager->m_list_score.size(); i++)
	{
		currScore += m_PlayManager->m_list_score.at(i);
	}

	lblScore->setString(String::createWithFormat("Score: %d", currScore)->getCString());
	int highScore = PlayerInfo::getInstance()->level->getLevelScore(LevelManager::s_mode, LevelManager::s_ID);
	if (highScore >= currScore)
	{

		background->getChildByName(GameSupport::getTextCongratulationComponent(ECongratulationComponent::kCongratulationComponent_lblHighScore))->setVisible(false);
	}

	m_Lose->setVisible(false);
	m_Win_1->setVisible(false);
	m_Win_n->setVisible(true);
}

void PlayCongratulationsLayer::btnBack_Tap(cocos2d::Ref *pSender)
{
	if (!isProcessing)
	{
        SoundManager::getInstance()->playSound(ESound::kSound_Click);
        
		runActionTimeline();
		auto func = CallFuncN::create(CC_CALLBACK_0(PlayCongratulationsLayer::returnLevelScene, this));
		m_nodeRoot->runAction(Sequence::create(DelayTime::create(1.0f), func, nullptr));
	}
}

void PlayCongratulationsLayer::btnNext_Tap(cocos2d::Ref *pSender)
{

	if (!isProcessing)
	{
        SoundManager::getInstance()->playSound(ESound::kSound_Click);
        
		isProcessing = true;
		int maxSize = 0;

		maxSize = (int)GameData::getInstance()->dataMode.size();
		

		if (LevelManager::s_ID + 1 == maxSize)
		{
			runActionTimeline();
			auto func = CallFuncN::create(CC_CALLBACK_0(PlayCongratulationsLayer::returnLevelScene, this));
			m_nodeRoot->runAction(Sequence::create(DelayTime::create(1.0f), func, nullptr));
		}
		else
		{
			LevelManager::s_ID++;
			runActionTimeline();
			auto func = CallFuncN::create(CC_CALLBACK_0(PlayCongratulationsLayer::reloadPlayManager, this));
			m_nodeRoot->runAction(Sequence::create(DelayTime::create(1.0f), func, nullptr));
		}

	}

}

void PlayCongratulationsLayer::btnReplay_Tap(cocos2d::Ref *pSender)
{
	
	if (!isProcessing)
	{
        SoundManager::getInstance()->playSound(ESound::kSound_Click);
        
		runActionTimeline();
		auto func = CallFuncN::create(CC_CALLBACK_0(PlayCongratulationsLayer::reloadPlayManager, this));
		m_nodeRoot->runAction(Sequence::create(DelayTime::create(1.0f), func, nullptr));
	}
}

void  PlayCongratulationsLayer::reloadPlayManager()
{
	m_PlayManager->clearCache();
	m_PlayManager->reload();
}

void PlayCongratulationsLayer::returnLevelScene()
{
	SceneManager::getInstance()->changeScene(m_nodeRoot, EScene::kScene_Level);
}
void PlayCongratulationsLayer::runActionTimeline()
{
	timeline::ActionTimeline *timeline = CSLoader::createTimeline("CongratulationLayer.csb");

	m_nodeRoot->runAction(timeline);
	timeline->gotoFrameAndPlay(400, 460, false);
}